package org.monitoringtools.examples;

import java.io.IOException;
import java.net.MalformedURLException;

import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.junit.Test;

public class JMXConnectionExample {

    @Test
    public void test() throws IOException {
        final String hostName = "localhost";
        final String portNum = "1510";

        final JMXConnector conn = getJMXConnector(hostName, portNum);

        System.out.println(conn);
        System.out.println(conn.getMBeanServerConnection());
    }

    private JMXConnector getJMXConnector(final String hostName, final String portNum) throws MalformedURLException, IOException {
        return JMXConnectorFactory.connect(new JMXServiceURL(getUrl(hostName, portNum)));
    }

    private String getUrl(final String hostName, final String portNum) {
        return String.format("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", hostName, portNum);
    }
}
